﻿using System;
using System.IO;
using MongoDB.Driver;
using Sustainalytics.DataLayer;
using Sustainalytics.Entities.Governance;
using Sustainalytics.Utils;

namespace Sustainalytics.Governance.BusinessLogic.Tests.Intergation_Tests
{
    public class GovernanceInputFileFixture : IDisposable
    {
        private readonly Guid _businessFileId;
        public Guid BusinessFileId
        {
            get { return _businessFileId; }
        }

        private readonly Guid _businessId;

        private readonly Guid _companyId;
        public Guid CompanyId
        {
            get { return _companyId; }
        }

        public Guid GovernanceFileId
        {
            get { return _governanceFileId; }
        }

        public Guid GovernanceId
        {
            get { return _governanceId; }
        }

        public static string GovernanceInputFileConnectionString
        {
            get { return _governanceInputFileConnectionString; }
        }

        private readonly Guid _governanceFileId;
        private readonly Guid _governanceId;
        private readonly string _fileName;
        private readonly IFileRepository _fileRepository;
        private static string _governanceInputFileConnectionString = "mongodb://localhost:27017/GovernanceInputFile";

        IMongoCollection<GovernancePdfProfileMetadata> _governancePdfProfileMetadataCollection;
        private readonly MongoGridFSHelper mongoGridFSHelper;

        private IMongoDatabase _database;

        public GovernanceInputFileFixture()
        {
            _businessId = Guid.Parse("c74ab81c-8abf-4939-9381-c6f5c44f5cd2");
            _businessFileId = Guid.Parse("020ecd44-3227-449b-a80f-c74fada65c83");

            _governanceFileId = Guid.Parse("f73c076f-f430-486e-88bd-8c4fb8501126");
            _governanceId = Guid.Parse("1a474e81-0041-4612-8f7d-774078d43580");

            _fileName = "Company Name";
            _companyId = Guid.Parse("080a19e9-8985-42fd-b28a-66ee8f913677");

            
            var mongoClient = new MongoClient(_governanceInputFileConnectionString);
            var url = new MongoUrl(_governanceInputFileConnectionString);

            _database = mongoClient.GetDatabase(url.DatabaseName);
            mongoGridFSHelper = new MongoGridFSHelper(mongoClient.GetDatabase(url.DatabaseName));

            _governancePdfProfileMetadataCollection = _database.GetCollection<GovernancePdfProfileMetadata>("GovernancePdfProfileMetadata");
            _fileRepository = new FileRepository(_governanceInputFileConnectionString);

            RemoveSeedData();

            SeedPdfMetadata();
            SeedPdfFile(_businessFileId, GetBusinessProfilePdfFile());
            SeedPdfFile(_governanceFileId, GetBusinessProfilePdfFile());
        }
        
        private void SeedPdfMetadata()
        {
            SeedBusinessData();
            SeedGovernanceData();
        }

        private void SeedBusinessData()
        {
            _governancePdfProfileMetadataCollection.InsertOne(new GovernancePdfProfileMetadata
            {
                Id = _businessId,
                FileId = _businessFileId,
                CompanyId = _companyId,
                OperationType = UpdateOperationType.Created,
                GovernancePdfType = GovernancePdfType.BusinessAndFinancialProfile,
                LastOperationUserName = "Ionut",
                LastOperationDateTime = DateTime.UtcNow,
                FileName = _fileName + UpdateOperationType.Created,
            });
        }

        private void SeedGovernanceData()
        {
            _governancePdfProfileMetadataCollection.InsertOne(new GovernancePdfProfileMetadata
            {
                Id = _governanceId,
                FileId = _governanceFileId,
                CompanyId = _companyId,
                OperationType = UpdateOperationType.Created,
                GovernancePdfType = GovernancePdfType.GovernanceDataProfile,
                LastOperationUserName = "Ionut",
                LastOperationDateTime = DateTime.UtcNow,
                FileName = _fileName + UpdateOperationType.Created,
            });
        }

        private void SeedPdfFile(Guid fileId, string filePath)
        {
            var baseUrl = "http://localhost:10280/governanceserv";
            var fileName = "file.pdf";
            var contentType = "application/pdf";

            var mongoData = _fileRepository.UploadFile(fileId, fileName, contentType);

            using (var file = ReadFile(filePath))
            {
                using (mongoData.Item1)
                {
                    file.CopyTo(mongoData.Item1);
                }
            }
        }

        private Stream ReadFile(string filePath)
        {
            return new FileStream(filePath, FileMode.Open);
        }
        public string GetBusinessProfilePdfFile()
        {
            var baseDirectory = AppDomain.CurrentDomain.BaseDirectory;
            return Path.Combine(baseDirectory, "Files", "Business and Financial Profile.pdf");
        }

        public string GetCorporateProfilePdfFile()
        {
            var baseDirectory = AppDomain.CurrentDomain.BaseDirectory;
            return Path.Combine(baseDirectory, "Files", "Governance Data.pdf");
        }

        private void RemoveSeedData()
        {
            _governancePdfProfileMetadataCollection.DeleteOne(x=> x.Id == _businessId);
            _governancePdfProfileMetadataCollection.DeleteOne(x=> x.Id == _governanceId);

            mongoGridFSHelper.DeleteFile(_businessFileId);
            mongoGridFSHelper.DeleteFile(_governanceFileId);
        }
        public void Dispose()
        {
            RemoveSeedData();
            GC.SuppressFinalize(this);
        }
    }
}
